1   /************************************************************
2   *                     Copyright                            *
3   * Portions of this software are Copyright (c) 1993 - 2002, *
4   * Chad Z. Hower (Kudzu) and the Indy Pit Crew              *
5   *  - http://www.nevrona.com/Indy/                          *
6   ************************************************************/
7   package org.indy;
8   
9   import java.io.File;
10  import java.io.FileNotFoundException;
11  import java.io.InputStream;
12  
13  import javax.swing.event.EventListenerList;
14  
15  import org.indy.io.IOHandler;
16  import org.indy.io.IOHandlerListener;
17  import org.indy.io.IOHandlerSocket;
18  import org.indy.io.IndyIOException;
19  import org.indy.io.MaxLineLengthExceededException;
20  import org.indy.io.NotConnectedException;
21  import org.indy.io.PeerDisconnectedException;
22  import org.indy.io.ReadTimedOutException;
23  
24  import org.indy.util.IndyUtilities;
25  import org.indy.util.StringList;
26  
27  
28  /***
29   *  Description of the Class
30   *
31   *@author  owen
32   */
33  public class TCPClient extends IndyComponent {
34    private final IOEventHandler ioEvents = new IOEventHandler();
35  
36    /*** DOCUMENT ME! */
37    protected EventListenerList listenerList = new EventListenerList();
38  
39    /***  Description of the Field  */
40    protected String host;
41  
42    /***  Description of the Field  */
43    protected int port = -1;
44  
45    /***  Description of the Field  */
46    protected String boundIP;
47  
48    /***  Description of the Field  */
49    protected int boundPort = 0;
50  
51    /***  Description of the Field  */
52    protected String username;
53  
54    /***  Description of the Field  */
55    protected String password;
56  
57    /*** DOCUMENT ME! */
58    protected IOHandlerSocket ioHandler;
59  
60    /*** DOCUMENT ME! */
61    protected Connection connection;
62  
63    /***  Constructor for the IdTCPClient object  */
64    public TCPClient() {
65      super();
66      ioHandler = new IOHandlerSocket();
67      connection = new Connection(ioHandler);
68    }
69  
70    private void doConnected(TCPClientEvent e) {
71      Object[] listeners = listenerList.getListenerList();
72  
73      for (int i = listeners.length - 2; i >= 0; i -= 2) {
74        if (listeners[i] == TCPClientListener.class) {
75          ((TCPClientListener) listeners[i + 1]).onConnect(e);
76        }
77      }
78    }
79  
80    private void doDisconnected(TCPClientEvent e) {
81      Object[] listeners = listenerList.getListenerList();
82  
83      for (int i = listeners.length - 2; i >= 0; i -= 2) {
84        if (listeners[i] == TCPClientListener.class) {
85          ((TCPClientListener) listeners[i + 1]).onDisconnected(e);
86        }
87      }
88    }
89  
90    /***
91     *  Description of the Method
92     *
93     *@throws  IdException Description of the Exception
94     */
95    public void connect() throws IndyException {
96      connect(IndyUtilities.getDefaultTimeout());
97    }
98  
99    /***
100    *  Description of the Method
101    *
102    *@param  timeout Description of the Parameter
103    *@throws  IdException Description of the Exception
104    */
105   public void connect(int timeout) throws IndyException {
106     if ((host != null) || (port != -1)) {
107       if (ioHandler == null) {
108         ioHandler = new IOHandlerSocket();
109         connection = new Connection(ioHandler);
110       }
111       else {
112         if (ioHandler.isConnected()) {
113           throw new IndyException(IndyUtilities.getResourceString(
114                                       "RSAlreadyConnected"));
115         }
116       }
117 
118       connection.resetConnection();
119       ioHandler.connectClient(host, port, boundIP, boundPort, timeout);
120     }
121     else {
122       throw new IllegalStateException(IndyUtilities.getResourceString(
123                                           "RSIllegalConnectState"));
124     }
125   }
126 
127   /***
128    *  Description of the Method
129    *
130    *@return  Description of the Return Value
131    */
132   public String connectAndGetAll() {
133     String res = null;
134 
135     try {
136       connect();
137       res = connection.allData();
138     }
139      catch (IndyException ie) {
140       ie.printStackTrace();
141     }
142      finally {
143       connection.disconnect();
144 
145       return res;
146     }
147   }
148 
149   /***
150    * DOCUMENT ME!
151    *
152    * @param failCount DOCUMENT ME!
153    *
154    * @return DOCUMENT ME!
155    *
156    * @throws IndyIOException DOCUMENT ME!
157    * @throws ReadTimedOutException DOCUMENT ME!
158    * @throws PeerDisconnectedException DOCUMENT ME!
159    * @throws MaxLineLengthExceededException DOCUMENT ME!
160    * @throws NotConnectedException DOCUMENT ME!
161    */
162   public String readLineWait(int failCount) throws IndyIOException, 
163                                                    ReadTimedOutException, 
164                                                    PeerDisconnectedException, 
165                                                    MaxLineLengthExceededException, 
166                                                    NotConnectedException {
167     return connection.readLineWait(failCount);
168   }
169 
170   /***
171    * DOCUMENT ME!
172    *
173    * @return DOCUMENT ME!
174    *
175    * @throws IndyIOException DOCUMENT ME!
176    * @throws ReadTimedOutException DOCUMENT ME!
177    * @throws PeerDisconnectedException DOCUMENT ME!
178    * @throws MaxLineLengthExceededException DOCUMENT ME!
179    * @throws NotConnectedException DOCUMENT ME!
180    */
181   public String readLineWait() throws IndyIOException, ReadTimedOutException, 
182                                       PeerDisconnectedException, 
183                                       MaxLineLengthExceededException, 
184                                       NotConnectedException {
185     return connection.readLineWait();
186   }
187 
188   /***
189    * DOCUMENT ME!
190    *
191    * @return DOCUMENT ME!
192    *
193    * @throws PeerDisconnectedException DOCUMENT ME!
194    * @throws ReadTimedOutException DOCUMENT ME!
195    * @throws IndyIOException DOCUMENT ME!
196    * @throws MaxLineLengthExceededException DOCUMENT ME!
197    * @throws NotConnectedException DOCUMENT ME!
198    */
199   public String readLine() throws PeerDisconnectedException, 
200                                   ReadTimedOutException, IndyIOException, 
201                                   MaxLineLengthExceededException, 
202                                   NotConnectedException {
203     return connection.readLine();
204   }
205 
206   /***
207    * DOCUMENT ME!
208    *
209    * @param timeout DOCUMENT ME!
210    * @param maxLineLength DOCUMENT ME!
211    *
212    * @return DOCUMENT ME!
213    *
214    * @throws IndyIOException DOCUMENT ME!
215    * @throws ReadTimedOutException DOCUMENT ME!
216    * @throws PeerDisconnectedException DOCUMENT ME!
217    * @throws NotConnectedException DOCUMENT ME!
218    * @throws MaxLineLengthExceededException DOCUMENT ME!
219    */
220   public String readLine(int timeout, int maxLineLength)
221                   throws IndyIOException, ReadTimedOutException, 
222                          PeerDisconnectedException, NotConnectedException, 
223                          MaxLineLengthExceededException {
224     return connection.readLine(timeout, maxLineLength);
225   }
226 
227   /***
228    * DOCUMENT ME!
229    *
230    * @param b DOCUMENT ME!
231    * @param len DOCUMENT ME!
232    *
233    * @throws PeerDisconnectedException DOCUMENT ME!
234    * @throws ReadTimedOutException DOCUMENT ME!
235    * @throws IndyIOException DOCUMENT ME!
236    * @throws NotConnectedException DOCUMENT ME!
237    */
238   public void readBuffer(byte[] b, int len) throws PeerDisconnectedException, 
239                                                    ReadTimedOutException, 
240                                                    IndyIOException, 
241                                                    NotConnectedException {
242     connection.readBuffer(b, len);
243   }
244 
245   /***
246    * DOCUMENT ME!
247    *
248    * @return DOCUMENT ME!
249    *
250    * @throws PeerDisconnectedException DOCUMENT ME!
251    * @throws ReadTimedOutException DOCUMENT ME!
252    * @throws IndyIOException DOCUMENT ME!
253    * @throws NotConnectedException DOCUMENT ME!
254    */
255   public int readInt() throws PeerDisconnectedException, ReadTimedOutException, 
256                               IndyIOException, NotConnectedException {
257     return connection.readInt();
258   }
259 
260   /***
261    * DOCUMENT ME!
262    *
263    * @return DOCUMENT ME!
264    *
265    * @throws PeerDisconnectedException DOCUMENT ME!
266    * @throws ReadTimedOutException DOCUMENT ME!
267    * @throws IndyIOException DOCUMENT ME!
268    * @throws NotConnectedException DOCUMENT ME!
269    */
270   public long readLong() throws PeerDisconnectedException, 
271                                 ReadTimedOutException, IndyIOException, 
272                                 NotConnectedException {
273     return connection.readLong();
274   }
275 
276   /***
277    * DOCUMENT ME!
278    *
279    * @return DOCUMENT ME!
280    *
281    * @throws PeerDisconnectedException DOCUMENT ME!
282    * @throws ReadTimedOutException DOCUMENT ME!
283    * @throws IndyIOException DOCUMENT ME!
284    * @throws NotConnectedException DOCUMENT ME!
285    */
286   public char readChar() throws PeerDisconnectedException, 
287                                 ReadTimedOutException, IndyIOException, 
288                                 NotConnectedException {
289     return connection.readChar();
290   }
291 
292   /***
293    * DOCUMENT ME!
294    *
295    * @return DOCUMENT ME!
296    *
297    * @throws PeerDisconnectedException DOCUMENT ME!
298    * @throws ReadTimedOutException DOCUMENT ME!
299    * @throws IndyIOException DOCUMENT ME!
300    * @throws NotConnectedException DOCUMENT ME!
301    */
302   public short readShort() throws PeerDisconnectedException, 
303                                   ReadTimedOutException, IndyIOException, 
304                                   NotConnectedException {
305     return connection.readShort();
306   }
307 
308   /***
309    * DOCUMENT ME!
310    *
311    * @param byteCount DOCUMENT ME!
312    * @param readUntilDisconnect DOCUMENT ME!
313    *
314    * @return DOCUMENT ME!
315    *
316    * @throws PeerDisconnectedException DOCUMENT ME!
317    * @throws ReadTimedOutException DOCUMENT ME!
318    * @throws NotConnectedException DOCUMENT ME!
319    * @throws IndyIOException DOCUMENT ME!
320    */
321   public InputStream readStream(int byteCount, boolean readUntilDisconnect)
322                          throws PeerDisconnectedException, 
323                                 ReadTimedOutException, NotConnectedException, 
324                                 IndyIOException {
325     return connection.readStream(byteCount, readUntilDisconnect);
326   }
327 
328   /***
329    * DOCUMENT ME!
330    *
331    * @param strings DOCUMENT ME!
332    *
333    * @throws IndyException DOCUMENT ME!
334    */
335   public void writeRFCStrings(StringList strings) throws IndyException {
336     connection.writeRFCStrings(strings);
337   }
338 
339   /***
340    * DOCUMENT ME!
341    *
342    * @param reply DOCUMENT ME!
343    *
344    * @throws IndyException DOCUMENT ME!
345    */
346   public void writeRFCReply(RFCReply reply) throws IndyException {
347     connection.writeRFCReply(reply);
348   }
349 
350   /***
351    * DOCUMENT ME!
352    *
353    * @param s DOCUMENT ME!
354    *
355    * @throws IndyIOException DOCUMENT ME!
356    * @throws PeerDisconnectedException DOCUMENT ME!
357    * @throws NotConnectedException DOCUMENT ME!
358    */
359   public void write(String s) throws IndyIOException, PeerDisconnectedException, 
360                                      NotConnectedException {
361     connection.write(s);
362   }
363 
364   /***
365    * DOCUMENT ME!
366    *
367    * @param buf DOCUMENT ME!
368    *
369    * @throws IndyIOException DOCUMENT ME!
370    * @throws PeerDisconnectedException DOCUMENT ME!
371    * @throws NotConnectedException DOCUMENT ME!
372    */
373   public void writeBuffer(byte[] buf) throws IndyIOException, 
374                                              PeerDisconnectedException, 
375                                              NotConnectedException {
376     connection.writeBuffer(buf);
377   }
378 
379   /***
380    * DOCUMENT ME!
381    *
382    * @param line DOCUMENT ME!
383    *
384    * @throws IndyIOException DOCUMENT ME!
385    * @throws PeerDisconnectedException DOCUMENT ME!
386    * @throws NotConnectedException DOCUMENT ME!
387    */
388   public void writeLine(String line) throws IndyIOException, 
389                                             PeerDisconnectedException, 
390                                             NotConnectedException {
391     connection.writeLine(line);
392   }
393 
394   /***
395    * DOCUMENT ME!
396    *
397    * @param header DOCUMENT ME!
398    *
399    * @throws PeerDisconnectedException DOCUMENT ME!
400    * @throws IndyIOException DOCUMENT ME!
401    * @throws NotConnectedException DOCUMENT ME!
402    */
403   public void writeHeader(StringList header) throws PeerDisconnectedException, 
404                                                     IndyIOException, 
405                                                     NotConnectedException {
406     connection.writeHeader(header);
407   }
408 
409   /***
410    * DOCUMENT ME!
411    *
412    * @param value DOCUMENT ME!
413    *
414    * @throws PeerDisconnectedException DOCUMENT ME!
415    * @throws IndyIOException DOCUMENT ME!
416    * @throws NotConnectedException DOCUMENT ME!
417    */
418   public void writeInt(int value) throws PeerDisconnectedException, 
419                                          IndyIOException, NotConnectedException {
420     connection.writeInt(value);
421   }
422 
423   /***
424    * DOCUMENT ME!
425    *
426    * @param value DOCUMENT ME!
427    *
428    * @throws PeerDisconnectedException DOCUMENT ME!
429    * @throws IndyIOException DOCUMENT ME!
430    * @throws NotConnectedException DOCUMENT ME!
431    */
432   public void writeLong(long value) throws PeerDisconnectedException, 
433                                            IndyIOException, 
434                                            NotConnectedException {
435     connection.writeLong(value);
436   }
437 
438   /***
439    * DOCUMENT ME!
440    *
441    * @param value DOCUMENT ME!
442    *
443    * @throws PeerDisconnectedException DOCUMENT ME!
444    * @throws IndyIOException DOCUMENT ME!
445    * @throws NotConnectedException DOCUMENT ME!
446    */
447   public void writeShort(short value) throws PeerDisconnectedException, 
448                                              IndyIOException, 
449                                              NotConnectedException {
450     connection.writeShort(value);
451   }
452 
453   /***
454    * DOCUMENT ME!
455    *
456    * @param strings DOCUMENT ME!
457    * @param writeLineCount DOCUMENT ME!
458    *
459    * @throws PeerDisconnectedException DOCUMENT ME!
460    * @throws IndyIOException DOCUMENT ME!
461    * @throws NotConnectedException DOCUMENT ME!
462    */
463   public void writeStrings(StringList strings, boolean writeLineCount)
464                     throws PeerDisconnectedException, IndyIOException, 
465                            NotConnectedException {
466     connection.writeStrings(strings, writeLineCount);
467   }
468 
469   /***
470    * DOCUMENT ME!
471    *
472    * @param stream DOCUMENT ME!
473    * @param len DOCUMENT ME!
474    * @param writeByteCount DOCUMENT ME!
475    *
476    * @throws PeerDisconnectedException DOCUMENT ME!
477    * @throws IndyIOException DOCUMENT ME!
478    * @throws NotConnectedException DOCUMENT ME!
479    */
480   public void writeStream(InputStream stream, int len, boolean writeByteCount)
481                    throws PeerDisconnectedException, IndyIOException, 
482                           NotConnectedException {
483     connection.writeStream(stream, len, writeByteCount);
484   }
485 
486   /***
487    * DOCUMENT ME!
488    *
489    * @param f DOCUMENT ME!
490    *
491    * @throws FileNotFoundException DOCUMENT ME!
492    * @throws PeerDisconnectedException DOCUMENT ME!
493    * @throws IndyIOException DOCUMENT ME!
494    * @throws NotConnectedException DOCUMENT ME!
495    */
496   public void writeFile(File f) throws FileNotFoundException, 
497                                        PeerDisconnectedException, 
498                                        IndyIOException, NotConnectedException {
499     connection.writeFile(f);
500   }
501 
502   /***
503    * DOCUMENT ME!
504    *
505    * @return DOCUMENT ME!
506    *
507    * @throws PeerDisconnectedException DOCUMENT ME!
508    * @throws IndyIOException DOCUMENT ME!
509    */
510   public byte[] currentReadBuffer() throws PeerDisconnectedException, 
511                                            IndyIOException {
512     return connection.currentReadBuffer();
513   }
514 
515   /***
516    * DOCUMENT ME!
517    *
518    * @return DOCUMENT ME!
519    *
520    * @throws PeerDisconnectedException DOCUMENT ME!
521    * @throws ReadTimedOutException DOCUMENT ME!
522    * @throws IndyIOException DOCUMENT ME!
523    * @throws NotConnectedException DOCUMENT ME!
524    */
525   public String allData() throws PeerDisconnectedException, 
526                                  ReadTimedOutException, IndyIOException, 
527                                  NotConnectedException {
528     return connection.allData();
529   }
530 
531   /***
532    * DOCUMENT ME!
533    *
534    * @throws IndyIOException DOCUMENT ME!
535    */
536   public void disconnect() throws IndyIOException {
537     connection.disconnect();
538 
539     //connection = null;
540     //ioHandler = null;
541   }
542 
543   /***
544    * DOCUMENT ME!
545    *
546    * @param l DOCUMENT ME!
547    */
548   public void addTCPClientListener(TCPClientListener l) {
549     listenerList.add(TCPClientListener.class, l);
550   }
551 
552   /***
553    * DOCUMENT ME!
554    *
555    * @param l DOCUMENT ME!
556    */
557   public void removeTCPClientListener(TCPClientListener l) {
558     listenerList.remove(TCPClientListener.class, l);
559   }
560 
561   /***
562    *  Sets the boundIP attribute of the IdTCPClient object
563    *
564    *@param  newBoundIP The new boundIP value
565    */
566   public void setBoundIP(String newBoundIP) {
567     boundIP = newBoundIP;
568   }
569 
570   /***
571    *  Sets the boundPort attribute of the IdTCPClient object
572    *
573    *@param  port The new boundPort value
574    */
575   public void setBoundPort(int port) {
576     boundPort = port;
577   }
578 
579   /***
580    *  Sets the host attribute of the IdTCPClient object
581    *
582    *@param  newHost The new host value
583    */
584   public void setHost(String newHost) {
585     host = newHost;
586   }
587 
588   /***
589    *  Sets the port attribute of the IdTCPClient object
590    *
591    *@param  newPort The new port value
592    */
593   public void setPort(int newPort) {
594     port = newPort;
595   }
596 
597   /***
598    *  Sets the username attribute of the IdTCPClient object
599    *
600    *@param  newUsername The new username value
601    */
602   public void setUsername(String newUsername) {
603     username = newUsername;
604   }
605 
606   /***
607    *  Sets the password attribute of the IdTCPClient object
608    *
609    *@param  newPassword The new password value
610    */
611   public void setPassword(String newPassword) {
612     password = newPassword;
613   }
614 
615   /***
616    * DOCUMENT ME!
617    *
618    * @param newLen DOCUMENT ME!
619    */
620   public void setMaximumLineLength(int newLen) {
621     connection.setMaxLineLength(newLen);
622   }
623 
624   /***
625    * DOCUMENT ME!
626    *
627    * @param timeout DOCUMENT ME!
628    *
629    * @throws IndyIOException DOCUMENT ME!
630    */
631   public void setReadTimeOut(int timeout) throws IndyIOException {
632     connection.setReadTimeOut(timeout);
633   }
634 
635   /***
636    * DOCUMENT ME!
637    *
638    * @param ioHandler DOCUMENT ME!
639    */
640   public void setIOHandler(IOHandlerSocket ioHandler) {
641     this.ioHandler = ioHandler;
642     connection = new Connection(ioHandler);
643   }
644 
645   /***
646    *  Gets the boundIP attribute of the IdTCPClient object
647    *
648    *@return  The boundIP value
649    */
650   public String getBoundIP() {
651     return boundIP;
652   }
653 
654   /***
655    *  Gets the boundPort attribute of the IdTCPClient object
656    *
657    *@return  The boundPort value
658    */
659   public int getBoundPort() {
660     try {
661       return ((IOHandlerSocket) ioHandler).getBoundPort();
662     }
663      catch (IndyException ie) {
664       return 0;
665     }
666   }
667 
668   /***
669    *  Gets the host attribute of the IdTCPClient object
670    *
671    *@return  The host value
672    */
673   public String getHost() {
674     return host;
675   }
676 
677   /***
678    *  Gets the port attribute of the IdTCPClient object
679    *
680    *@return  The port value
681    */
682   public int getPort() {
683     return port;
684   }
685 
686   /***
687    *  Gets the username attribute of the IdTCPClient object
688    *
689    *@return  The username value
690    */
691   public String getUsername() {
692     return username;
693   }
694 
695   /***
696    *  Gets the password attribute of the IdTCPClient object
697    *
698    *@return  The password value
699    */
700   public String getPassword() {
701     return password;
702   }
703 
704   /***
705    * DOCUMENT ME!
706    *
707    * @return DOCUMENT ME!
708    */
709   public int getMaximumLineLength() {
710     return connection.getMaximumLineLength();
711   }
712 
713   /***
714    * DOCUMENT ME!
715    *
716    * @return DOCUMENT ME!
717    *
718    * @throws IndyIOException DOCUMENT ME!
719    */
720   public int getReadTimeOut() throws IndyIOException {
721     return connection.getReadTimeOut();
722   }
723 
724   /***
725    * DOCUMENT ME!
726    *
727    * @return DOCUMENT ME!
728    */
729   public boolean isConnected() {
730     return (connection == null) ? false : connection.isConnected();
731   }
732 
733   /***
734    * DOCUMENT ME!
735    *
736    * @return DOCUMENT ME!
737    */
738   public IOHandlerSocket getIOHandler() {
739     return ioHandler;
740   }
741 
742   private class IOEventHandler implements IOHandlerListener {
743     TCPClientEvent e = new TCPClientEvent(TCPClient.this);
744 
745     public void onConnect(IOHandler sender) {
746       doConnected(e);
747     }
748 
749     public void onDisconnect(IOHandler sender) {
750       doDisconnected(e);
751     }
752 
753     public void onStatus(IOHandler sender, Status status, Object[] args) {
754       doStatus(status, args);
755     }
756   }
757 }
This page was automatically generated by Maven